import request from '../utils/request';
import { useUserStore } from '../store/user';

/**
 * 发送新讨论
 */
export async function sendDiscussion(params: {
  title: string;
  content: string;
}) {
  try {
    const result = await request({
      url: '/discussion/send',
      method: 'POST',
      data: params
    });
    
    return result;
  } catch (error) {
    console.error('发送讨论失败:', error);
    throw error;
  }
}

/**
 * 获取讨论列表
 */
export async function fetchDiscussionList(params: {
  pageNum: number;
  pageSize: number;
  sortBy?: string;
  orderBy?: string;
}) {
  try {
    const result = await request({
      url: '/discussion/list',
      method: 'GET',
      params: params
    });
    
    return result;
  } catch (error) {
    console.error('获取讨论列表失败:', error);
    throw error;
  }
}

/**
 * 获取讨论详情
 */
export async function getDiscussionDetail(id: number) {
  try {
    const result = await request({
      url: `/discussion/${id}`,
      method: 'GET'
    });
    
    return result;
  } catch (error) {
    console.error('获取讨论详情失败:', error);
    throw error;
  }
}

/**
 * 删除讨论
 */
export async function deleteDiscussion(id: number) {
  try {
    const result = await request({
      url: `/discussion/delete`,
      method: 'DELETE',
      params: { id }
    });
    
    return result;
  } catch (error) {
    console.error('删除讨论失败:', error);
    throw error;
  }
}

/**
 * 点赞讨论
 */
export async function likeDiscussion(discussionId: number) {
  try {
    const result = await request({
      url: `/discussion/${discussionId}/like`,
      method: 'POST'
    });
    
    return result;
  } catch (error) {
    console.error('点赞讨论失败:', error);
    throw error;
  }
}

/**
 * 取消点赞讨论
 */
export async function unlikeDiscussion(discussionId: number) {
  try {
    const result = await request({
      url: `/discussion/${discussionId}/unlike`,
      method: 'POST'
    });
    
    return result;
  } catch (error) {
    console.error('取消点赞讨论失败:', error);
    throw error;
  }
}

/**
 * 发送回复
 */
export async function sendReply(params: {
  content: string;
  toCid: number;
  toName?: string;
}) {
  try {
    const result = await request({
      url: '/discussion/sendReply',
      method: 'POST',
      data: params
    });
    
    return result;
  } catch (error) {
    console.error('发送回复失败:', error);
    throw error;
  }
}

/**
 * 获取回复列表
 */
export async function fetchReplyList(id: number, params: {
  pageNum: number;
  pageSize: number;
  sortBy?: string;
  orderBy?: string;
}) {
  try {
    const result = await request({
      url: `/discussion/listReply/${id}`,
      method: 'GET',
      params: params
    });
    
    return result;
  } catch (error) {
    console.error('获取回复列表失败:', error);
    throw error;
  }
}

/**
 * 删除回复
 */
export async function deleteReply(id: number) {
  try {
    const result = await request({
      url: `/discussion/deleteReply`,
      method: 'DELETE',
      params: { id }
    });
    
    return result;
  } catch (error) {
    console.error('删除回复失败:', error);
    throw error;
  }
}

/**
 * 收藏讨论
 */
export async function collectDiscussion(discussionId: number) {
  try {
    const result = await request({
      url: `/discussion/${discussionId}/collect`,
      method: 'POST'
    });
    
    return result;
  } catch (error) {
    console.error('收藏讨论失败:', error);
    throw error;
  }
}

/**
 * 取消收藏讨论
 */
export async function uncollectDiscussion(discussionId: number) {
  try {
    const result = await request({
      url: `/discussion/${discussionId}/collect`,
      method: 'DELETE'
    });
    
    return result;
  } catch (error) {
    console.error('取消收藏讨论失败:', error);
    throw error;
  }
}

/**
 * 获取收藏状态
 */
export async function getCollectionStatus(discussionId: number) {
  try {
    const result = await request({
      url: `/discussion/${discussionId}/collect/status`,
      method: 'GET'
    });
    
    return result;
  } catch (error) {
    console.error('获取收藏状态失败:', error);
    throw error;
  }
}

/**
 * 获取收藏的讨论列表
 */
export async function getCollectedDiscussions(params: {
  page: number;
  size: number;
  reverseOrder?: boolean;
}) {
  try {
    const result = await request({
      url: `/discussion/collect`,
      method: 'GET',
      params: params
    });
    
    return result;
  } catch (error) {
    console.error('获取收藏的讨论列表失败:', error);
    throw error;
  }
}

/**
 * 获取收藏数量
 */
export async function getCollectedCount() {
  try {
    const result = await request({
      url: `/discussion/collect/count`,
      method: 'GET'
    });
    
    return result;
  } catch (error) {
    console.error('获取收藏数量失败:', error);
    throw error;
  }
} 